import {
    getalltalents,
    getalltalentsAdmin,
    saveorupdate,
    updatetalent,
    getmytalent,
    gettalent,
    querycandidatesbytalent,
    generatecandidate,
    getDict,
    querylistbypost
} from '@/services/myRecruit/talentPool';

export default {
    namespace: 'talentPool',
    state: {
        myTalentPoolList: [],
        jobApplication: [],
        candidateInfo: {
            // id: '',
            // insertDate: '',
            // lastCompany: '',
            // lastPost: '',
            // notesSource: '',
            // notesSourceDetail: '',
            // talentBirth: '',
            // talentDegree: '',
            // talentEmail: '',
            // talentGraduate: '',
            // talentGraduateSchool: '',
            // talentLeaderId: '',
            // talentLeaderName: '',
            // talentName: '',
            // talentNationalArea: '',
            // talentPhone: '',
            // talentProfessional: '',
            // talentSex: '',
            // talentTag: '',
            // resourceRelation: ''
        },
        candidateInfoEdit: {},
        candidateSexTypeList: [], // 性别字典
        candidateAgreeTypeList: [], // 学历字典
        chinaRecruitWayList: [], // 厦门招聘渠道
        singaporeRecruitWayList: [], // 新加坡招聘渠道
        asianList: [], // 籍贯
        querylistList: [],

        pagination: {
            pageSize: 10,
            current: 1,
            total: 0
        },
        orderBy: ''
    },
    effects: {
        // 获取人才库列表 -普通人
        *getalltalents({ payload }, { call, put }) {
            const result = yield call(getalltalents, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        myTalentPoolList: result.data.rows,
                        pagination: {
                            current: result.data.pageNo,
                            pageSize: result.data.pageSize,
                            total: result.data.total
                        }
                    }
                });
            }
        },
        // 获取人才库列表 -管理者
        *getalltalentsAdmin({ payload }, { call, put }) {
            const result = yield call(getalltalentsAdmin, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        myTalentPoolList: result.data.rows,
                        pagination: {
                            current: result.data.pageNo,
                            pageSize: result.data.pageSize,
                            total: result.data.total
                        }
                    }
                });
            }
        },
        // 添加人才
        *saveorupdate({ payload, callback }, { call }) {
            const result = yield call(saveorupdate, payload);
            callback(result);
        },
        // 更新
        *updatetalent({ payload, callback }, { call }) {
            const result = yield call(updatetalent, payload);
            callback(result);
        },
        // 编辑人才库信息
        *getone({ payload, callback }, { call, put }) {
            let result;
            if (payload.identity === 'admin') {
                result = yield call(gettalent, payload);
            } else {
                result = yield call(getmytalent, payload);
            }
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        candidateInfoEdit: result.data
                    }
                });
                if (callback) callback(result.data);
            }
        },
        // 查询简历相关候选人信息
        *querycandidatesbytalent({ payload }, { call, put }) {
            const result = yield call(querycandidatesbytalent, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        jobApplication: result.data
                    }
                });
            }
        },
        // 性别字典和学历字典
        // 招聘渠道 厦门/新加坡
        *getDict(_, { call, put, all }) {
            const [
                candidateSexType,
                candidateAgreeType,
                singaporeRecruitWay,
                chinaRecruitWay
            ] = yield all([
                call(getDict, { code: 'candidateSexType' }),
                call(getDict, { code: 'agreeType' }),
                call(getDict, { code: 'SingaporeRecruitWay' }),
                call(getDict, { code: 'ChinaRecruitWay' })
            ]);
            if (candidateSexType.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        candidateSexTypeList: candidateSexType.data
                    }
                });
            }
            if (candidateAgreeType.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        candidateAgreeTypeList: candidateAgreeType.data
                    }
                });
            }
            if (chinaRecruitWay.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        chinaRecruitWayList: chinaRecruitWay.data
                    }
                });
            }
            if (singaporeRecruitWay.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        singaporeRecruitWayList: singaporeRecruitWay.data
                    }
                });
            }
        },
        // 查询简历相关候选人信息
        *querylist({ payload, callback }, { call, put }) {
            const result = yield call(querylistbypost, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        querylistList: result.data
                    }
                });
                callback();
            }
        },
        // 查询简历相关候选人信息
        *generatecandidate({ payload, callback }, { call }) {
            const result = yield call(generatecandidate, payload);
            callback(result);
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    }
};
